﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace _00_连接数据库
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //准备SQL的连接语句，连接到本地的数据库
            //地址:DS16PLUS
            //数据库:TEST5
            //账号:sa
            //密码:123456
            //信任服务器连接:True
            string connectionString = "Data Source=DS16PLUS;Initial Catalog=TEST5;User ID=sa;Password=123456;Trusted_Connection=True";
            
            //创建连接字符串构建器,用来提供连接SQLServer数据库
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = "DS16PLUS";
            builder.InitialCatalog = "TEST5";
            builder.UserID = "sa";
            builder.Password = "123456";
            
            //创建连接对象
            using(SqlConnection connection = new SqlConnection(builder.ToString()))
            {
                try
                {
                    connection.Open();
                    Console.WriteLine("连接成功");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("连接失败:"+ex.Message);
                }
            }

            Console.Read();

        }
    }
}
